window.onload = function () {
    let size = 5;
    let page = 1;
    let paeStr = "size=" + size + "&page=" + page;
    let dateObj = {};
    let tBodyObj = my$("tb")
    let spanObj = my$("pags");
    showMsee();

    function showMsee() {
        ajax("student/list.php", "POST", paeStr, show);

        function show(jsonObj) {
            //总数量
            let stuDate = jsonObj.data;
            dateObj.count = stuDate.count;
            dateObj.stuArr = stuDate.list;


            //创建信息前先删除

            // let tbChild = tBodyObj.children;
            // for (let j = 0; j < tbChild.length;) {
            //     tBodyObj.removeChild(tbChild[j]);
            // }
            removeChil(tBodyObj);
            removeChil(spanObj);

            function removeChil(obj) {
                let child = obj.children;
                for (let j = 0; j < child.length;) {
                    obj.removeChild(child[j]);
                }
            }


            //创建信息
            for (let i = 0; i < size; i++) {
                let trObj = document.createElement("tr");
                for (let key in dateObj.stuArr[i]) {
                    var tdObj = document.createElement("td");
                    if (key == "sex") {
                        if (dateObj.stuArr[i][key] == 1) {
                            tdObj.innerText = "男";
                        } else {
                            tdObj.innerText = "女";
                        }
                    } else if (key == "city") {
                        switch (dateObj.stuArr[i][key]) {
                            case "1":
                                tdObj.innerText = "北京";
                                break;
                            case "2":
                                tdObj.innerText = "福州";
                                break;
                            case "3":
                                tdObj.innerText = "厦门";
                                break;
                            case "4":
                                tdObj.innerText = "龙岩";
                                break;
                            case "5":
                                tdObj.innerText = "泉州";
                                break;

                        }
                    }else {
                        tdObj.innerText = dateObj.stuArr[i][key];
                    }
                    trObj.appendChild(tdObj);
                }
                tBodyObj.appendChild(trObj);
            }

            //创建页码
            //创建页码
            for (let k = 1; k <=Math.ceil(dateObj.count / size); k++) {
                let btn = document.createElement("button");
                btn.innerText = k;
                spanObj.appendChild(btn);
            }

            let pageArr = spanObj.children;
            for (let i = 0; i < pageArr.length; i++) {
                pageArr[i].onclick = function (e) {
                    e.stopPropagation();
                    page = i + 1;
                    paeStr = "size=" + size + "&page=" + page;
                    showMsee();
                }
            }
        }
    }

    let choose = my$("nums");
    choose.addEventListener("click", function (e) {
        let target = e.target;
        switch (target.id) {
            case "right":
                if (page >= Math.ceil(dateObj.count / size) - 1) {
                    page = Math.ceil(dateObj.count / size) - 1;
                    break;
                }
                page++;
                paeStr = "size=" + size + "&page=" + page;
                showMsee();
                break;
            case "left":
                if (page <= 1) {
                    page = 1;
                    break;
                }
                page--;
                paeStr = "size=" + size + "&page=" + page;
                showMsee();
                break;
        }
    });

};



    
    

